package com.egao.oa.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.oa.biz.ScheduleBiz;
import com.egao.oa.entity.Oa_schedule;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2020-09-18 14:18:11
 * @version ：1.0
 */
@Repository
public class ScheduleBizImpl extends BaseDao implements ScheduleBiz {

	@Override
    public JsonResult list(PageResult2<T> page, Oa_schedule oa_schedule, User loginUser) throws Exception {
        Map<String, String> map  = new HashMap<String, String>();
        RowMapper<Oa_schedule> rm = new BeanPropertyRowMapper<Oa_schedule>(Oa_schedule.class);
        StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.content");
        sql.append(",o.type,o.scheduleTime,o.create_time,o.create_user");
		sql.append(",o.update_time,o.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
        sql.append(" from oa_schedule o ");
        sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id ");
        sql.append(" where o.deleted=0 and sogzn.deleted=0 ");
        if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and o.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if(Utils.isNotEmpty(oa_schedule)){
//			if(Utils.isNotEmpty(oa_schedule.getName())){
//				sql.append(" and instr(o.name,:name)>0 ");
//				map.put("name", oa_schedule.getName());
//			}
            if(Utils.isNotEmpty(oa_schedule.getSort())&&Utils.isNotEmpty(oa_schedule.getOrder())){
                sql.append(" order by o."+oa_schedule.getSort()+" "+oa_schedule.getOrder());
            } else {
                sql.append(" order by o.create_time desc");
            }
        }
        return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
    }

    @Override
    public List<Oa_schedule> list(Oa_schedule oa_schedule, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        RowMapper<Oa_schedule> rm = new BeanPropertyRowMapper<Oa_schedule>(Oa_schedule.class);
        StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.content");
        sql.append(",o.type,o.scheduleTime,o.create_time,o.create_user");
		sql.append(",o.update_time,o.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
        sql.append(" from oa_schedule o ");
        sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id ");
        sql.append(" where o.deleted=0 and sogzn.deleted=0 ");
        if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and o.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if(Utils.isNotEmpty(oa_schedule)){
//			if(Utils.isNotEmpty(oa_schedule.getName())){
//				sql.append(" and instr(o.name,:name)>0 ");
//				map.put("name", oa_schedule.getName());
//			}
            if(Utils.isNotEmpty(oa_schedule.getSort())&&Utils.isNotEmpty(oa_schedule.getOrder())){
                sql.append(" order by o."+oa_schedule.getSort()+" "+oa_schedule.getOrder());
            } else {
                sql.append(" order by o.create_time desc");
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(),map, rm);
    }

    @Override
    public Oa_schedule getById(Oa_schedule oa_schedule) {
        RowMapper<Oa_schedule> rm = new BeanPropertyRowMapper<Oa_schedule>(Oa_schedule.class);
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.content");
        sql.append(",o.type,o.scheduleTime,o.create_time,o.create_user");
		sql.append(",o.update_time,o.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
        sql.append(" from oa_schedule o ");
        sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id ");
        sql.append(" where o.deleted=0 and sogzn.deleted=0 and o.id=:id ");
        map.put("id", oa_schedule.getId());
        getMapParameter(sql, map);
        return jdbcTemplate.queryForObject(sql.toString(), map, rm);
    }

    @Override
    public boolean save(Oa_schedule oa_schedule, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql =new StringBuffer("insert into oa_schedule (");
        sql.append("organization_id,content,type,scheduleTime");
		sql.append(",create_user,create_time) values ( ");
        sql.append(":organization_id,:content,:type,:scheduleTime");
		sql.append(",:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("content", oa_schedule.getContent());
        map.put("type", oa_schedule.getType());
        map.put("scheduleTime", oa_schedule.getScheduletime());
        map.put("create_user", loginUser.getUserId() + "");
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean update(Oa_schedule oa_schedule, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql =new StringBuffer("update oa_schedule set ");
        sql.append("content=:content,type=:type,scheduleTime=:scheduleTime ");
		sql.append(",update_time=now(),update_user=:update_user where id=:id ");
        map.put("content", oa_schedule.getContent());
        map.put("type", oa_schedule.getType());
        map.put("scheduleTime", oa_schedule.getScheduletime());
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", oa_schedule.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean remove(Oa_schedule oa_schedule, User loginUser) {
        int count = 0;
        Map<String, String> map = null;
        if(oa_schedule.getId().length()>0){
            for(String ID : oa_schedule.getId().split(",")){
                map  = new HashMap<String, String>();
                if(ID==null || ID.equals("")){ break; }
                StringBuffer sql =new StringBuffer("update oa_schedule set deleted=1" +
                        ",update_user=:update_user, update_time=now() "
                        + " where id =:id ");
                map.put("update_user", loginUser.getUserId() + "");
                map.put("id", ID);
                getMapParameter(sql, map);
                count = jdbcTemplate.update(sql.toString(),map);
            }
        }
        return count > 0;
    }
}